package com.my.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.my.constant.Common;
import com.my.entity.Employee;
import com.my.service.EmployeeService;
import com.my.util.CheckCorrect;
import com.my.util.CookieUtil;
import com.my.util.StringUtil;

@Controller
@RequestMapping(value = "/user")
public class LoginAction  {
	private static final Logger logger = LoggerFactory.getLogger(LoginAction.class);
	@Resource
	private EmployeeService employeeService;
	/**
	 * 登陆
	 * @param name
	 * @param password
	 * @param autoLogin
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String userLogin(
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "password", required = true) String password,
			@RequestParam(value = "autoLogin", required = false) String autoLogin,
			HttpServletRequest request,HttpServletResponse response) {
		Employee employee=employeeService.login(name, password);
		if(null!=employee){
			if("on".equals(autoLogin)){
				CookieUtil.saveCookie(employee, response);
			}
			request.getSession().setAttribute(Common.CURRENT_OPERATOR.getValue(), employee);
			StringBuffer visitUrl=(StringBuffer) request.getSession().getAttribute("visitUrl");
			if(null!=visitUrl && StringUtil.judgeStringIfNull(new String[]{visitUrl.toString()}) && CheckCorrect.checkUrl(visitUrl.toString())){
				return "redirect:"+visitUrl.toString();
			}else{
				String basePath = (String) request.getSession().getAttribute("basePath");
				return "redirect:"+basePath+"index/index.do";
			}
		}else{
			return "common/error";
		}
	}
	
	/**
	 * 注册
	 * @param request
	 * @param employee
	 * @param hobby
	 * @return
	 */
	@RequestMapping(value = "register")
	public String register(HttpServletRequest request,Employee employee,String[] hobby) {
		Employee emp=(Employee) request.getSession().getAttribute(Common.CURRENT_OPERATOR.getValue());
		employeeService.register(employee,emp.getName(),hobby);
		String basePath = (String) request.getSession().getAttribute("basePath");
		return "redirect:"+basePath+"emp/emp_list_for_page.do";
	}
	
	/**
	 * 注销
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "login_out")
	public String loginOut(HttpServletRequest request,HttpServletResponse response) {
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		request.getSession().invalidate();
		CookieUtil.clearCookie(response);
		return "redirect:"+basePath+"index/to_login.do";
	}
}
